<?php

namespace app\stores\controller\admin\cash;

use app\BaseController;
use app\cash\model\GoodsModel;
use app\cash\model\ServiceFeeModel;
use app\stores\model\cash\CashOrderSetModel;
use think\facade\Db;

class OrderSet extends BaseController{
    /**
     * 收银显示
     */
    public function cashShow()

    {
        $where['shop_id'] = $this->request->shopId;
        $where['type'] = 1;
        $where['is_delete'] = 0;
        $set = CashOrderSetModel::info($where);
        if(empty($set)){
            $data['shop_id'] = $this->request->shopId;
            $data['type'] = 1;
            $data['edit_time'] = date('Y-m-d H:i:s');
            $data['add_ip'] = request()->ip();
            CashOrderSetModel::insertGetId($data);
            $set = CashOrderSetModel::info($where);
        }
        $set['service_fee_list'] = [];
        if($set['service_fee_type']==1){
            $set['service_fee_data'] = ServiceFeeModel::list(['shop_id'=>$this->request->shopId,'is_delete'=>0],['add_time'=>'asc']);
        }
        $set['support_service_type'] = $set['support_service_type']?array_map('intval',explode(',',$set['support_service_type'])):null;

        return $this->success($set);

    }
    /**
     * 扫码点餐显示
     */
    public function codeShow()

    {
        $where['shop_id'] = $this->request->shopId;
        $where['type'] = 2;
        $where['is_delete'] = 0;
        $set = CashOrderSetModel::info($where);
        if(empty($set)){
            $data['shop_id'] = $this->request->shopId;
            $data['type'] = 2;
            $data['edit_time'] = date('Y-m-d H:i:s');
            $data['add_ip'] = request()->ip();
            CashOrderSetModel::insertGetId($data);
            $set = CashOrderSetModel::info($where);
        }
        $set['support_service_type'] = $set['support_service_type']?array_map('intval',explode(',',$set['support_service_type'])):null;

        return $this->success($set);

    }
    /**
     * 收银修改
     */
    public function cashSave()

    {
        $settle_type = $this->request->param('settle_type/d',1);
        $support_service_type = $this->request->param('support_service_type/a',[]);
        $service_fee_type = $this->request->param('service_fee_type/d',0);
        $service_fee_data = $this->request->param('service_fee_data/a',[]);
        $day_quota_type = $this->request->param('day_quota_type/d',1);
        $day_quota = $this->request->param('day_quota/d',0);

        if(!in_array(1,$support_service_type)) return $this->error('堂食默认必选');

        $where['shop_id'] = $this->request->shopId;
        $where['type'] = 1;
        $where['is_delete'] = 0;
        $set = CashOrderSetModel::info($where);

        Db::startTrans();
        try{
            $where['shop_id'] = $this->request->shopId;
            $where['type'] = 1;
            $where['is_delete'] = 0;

            if($service_fee_type==1){
                $old_service = ServiceFeeModel::where($where)->select()->toArray();
                $update_fee_ids = array_column($service_fee_data,'fee_id') ?? []; $delete_service=[];
                $old_fee_ids = array_column($old_service,'fee_id') ?? [];
                foreach ($old_service as $k => $v){
                    if(!in_array($v['fee_id'],$update_fee_ids)){
                        $v['is_delete'] = 1;
                        $delete_service[] = $v;
                    }
                }

                foreach ($service_fee_data as $k => $v){
                    $service_fee_data[$k]['shop_id'] = $this->request->shopId;
                    $service_fee_data[$k]['table_number_ids'] = implode(',',$v['table_number_ids']);
                    if(isset($v['fee_id'])){
                        if(!in_array($v['fee_id'],$old_fee_ids)) unset($service_fee_data[$k]);
                    }
                }
                $new_service = array_merge($service_fee_data,$delete_service);
                //删除
                $sf_model = new ServiceFeeModel();
                $sf_model->saveAll($new_service);
            }

            $data['settle_type'] = $settle_type;
            $data['support_service_type'] = implode(',',$support_service_type);
            $data['service_fee_type'] = $service_fee_type;
            $data['day_quota_type'] = $day_quota_type;
            if($day_quota_type==2) $data['day_quota'] = $day_quota;

            CashOrderSetModel::up($data,$where);
            if($day_quota_type==1 && $set['day_quota_type']!=1){
                GoodsModel::up(['quota'=>999],['shop_id'=>$this->request->shopId,'origin_type'=>2,'is_delete'=>0]);
            }
            if($day_quota_type==2){
                GoodsModel::up(['quota'=>$day_quota],['shop_id'=>$this->request->shopId,'origin_type'=>2,'is_delete'=>0]);
            }

            Db::commit();
        }catch(\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }
        return $this->success('保存成功');

    }
    /**
     * 扫码点餐修改
     */
    public function codeSave()

    {
        $status = $this->request->param('status/d',0);
        $settle_type = $this->request->param('settle_type/d',1);
        $support_service_type = $this->request->param('support_service_type/a',[]);
        $is_dinner_num = $this->request->param('is_dinner_num/d',0);

        if(!in_array(1,$support_service_type)) return $this->error('堂食默认必选');

        Db::startTrans();
        try{
            $where['shop_id'] = $this->request->shopId;
            $where['type'] = 2;
            $where['is_delete'] = 0;

            $data['status'] = $status;
            $data['settle_type'] = $settle_type;
            $data['support_service_type'] = implode(',',$support_service_type);
            $data['is_dinner_num'] = $is_dinner_num;

            CashOrderSetModel::up($data,$where);

            Db::commit();
        }catch(\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }
        return $this->success('保存成功');

    }
}

